package com.gaber.test;

import java.io.*;

public class Stream_File_IO {
    public static void main(String[] args) throws IOException {
        System.out.println("输出流IO");
//        BRRead();
//        WriteDemo();
//        fileStreamTest();
//        fileStreamTest2();
//        DirList();
        FileRead();
//        BRReadLines();
    }

    public static void BRRead() throws IOException {
        char c;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("输入字符，按下'q'键退出。");
        // 读取字符
        do {
            c = (char) br.read();
            System.out.println(c);
        } while (c != 'q');

    }

    public static void BRReadLines() {
        // 使用 System.in 创建 BufferedReader
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        System.out.println("Enter lines of text.");
        System.out.println("Enter 'end' to quit.");
        do {
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println(str);
        } while (!str.equals("end"));
    }

    public static void WriteDemo() {
        int b;
        b = 'A';
        System.out.write(b);
        System.out.write('\n');
    }

    public static void fileStreamTest() {
        try {
            byte bWrite[] = {11, 21, 3, 40, 5};
            FileOutputStream os = new FileOutputStream("c:/java/hello.txt");
            for (int x = 0; x < bWrite.length; x++) {
                os.write(bWrite[x]);
            }

            FileInputStream is = new FileInputStream("c:/java/hello.txt");
            int size = is.available();
            for (int i = 0; i < size; i++) {
                System.out.print(is.read() + " ");
            }
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void fileStreamTest2() throws IOException {
        File f = new File("a.txt");
        FileOutputStream fop = new FileOutputStream(f);
        // 构建FileOutputStream对象，文件不存在会自动新建
        OutputStreamWriter writer = new OutputStreamWriter(fop, "UTF-8");
        // 构建OutputStreamWriter对象, 参数可以指定编码，默认为操作系统默认编码，Windows上是gbk

        writer.append("中文输入");
        // 写入到缓冲区

        writer.append("\r\n");
        // 换行

        writer.append("English");
        // 刷新缓冲，写入到文件，如果下面没有写入内容了，直接close也会写入

        writer.close();
        // 关闭写入流，同时会把缓冲区内容写入文件

        fop.close();
        // 关闭输出流，释放系统资源

        FileInputStream fip = new FileInputStream(f);
        // 构建FileInputStream对象

        InputStreamReader render = new InputStreamReader(fip, "UTF-8");
        // 构建InputDtreamRender对象，编码与写入相同

        StringBuffer sb = new StringBuffer();

        while (render.ready()) {
            sb.append((char) render.read());
            // 转成char到StringBuffer对象中
        }
        System.out.println(sb.toString());

        render.close();
        // 关闭读取流

        fip.close();
        // 关闭输入流，释放系统资源
    }

    //创建目录
    public static void CreateDir() {
        String dirname = "/tmp/user/java/bin";
        File d = new File(dirname);
        // 现在创建目录
        d.mkdirs();
    }

    // 读取目录
    public static void DirList() {
        String dirname = "/java";
        File f1 = new File(dirname);
        if (f1.isDirectory()) {
            System.out.println("Directory of " + dirname);
            String s[] = f1.list();

            for (int i = 0; i < s.length; i++) {
                File f = new File(dirname + "/" + s[i]);
                if (f.isDirectory()) {
                    System.out.println(s[i] + " is a directory");
                } else {
                    System.out.println(s[i] + " is a file");
                }
            }
        } else {
            System.out.println(dirname + " is not a directory");
        }

    }

    // FileReader类从InputStreamReader类继承而来。该类按字符读取流中数据。可以通过以下几种构造方法创建需要的对象。
    public static void FileRead() throws IOException {
        File file = new File("hello1.txt");
        // 创建文件
        file.createNewFile();
        FileWriter writer = new FileWriter(file);

        writer.write("this\nis\nan\nexample\n");
        writer.flush();
        writer.close();

        FileReader fr = new FileReader(file);
        char[] a = new char[50];
        fr.read(a);
        for (char c : a)
            System.out.print(c);
        fr.close();
    }


}


